//
//  SwiftCollectionViewController.swift
//  SwiftTest
//
//  Created by 李江湖 on 2018/7/18.
//  Copyright © 2018年 李江湖. All rights reserved.
//

import UIKit

class SwiftCollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    var dataSource = [String]()
    //懒加载
    lazy var collectionView : UICollectionView  = {
        let collLayou = UICollectionViewFlowLayout()
        let mag : CGFloat = 10.0
        let count : CGFloat = 5.0
        let weight = (ScreenWidth-(mag*(count+1)))/count
        collLayou.itemSize = CGSize(width:weight, height: 35)
        //行间距
        collLayou.minimumInteritemSpacing = 5
        //列间距
//        collLayou.minimumLineSpacing = 10
        
        collLayou.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
        let coll = UICollectionView(frame: self.view.bounds, collectionViewLayout: collLayou)
        coll.backgroundColor = UIColor.white
        return coll
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.view.backgroundColor = UIColor.white
        self.view.addSubview(self.collectionView)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        
        
//        self.collectionView.register(UINib.init(nibName: NSStringFromClass(SwiftCollectionViewCell.classForCoder()), bundle: nil), forCellWithReuseIdentifier: "cell")
        self.collectionView.register(SwiftCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        saveData()
    }
    
    //添加数据
    private func saveData() {
        dataSource += ["A-1","A-2","A-3","A-4","A-5","A-6","A-7","A-8","A-9","A-10","A-11"]
        print(dataSource)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SwiftCollectionViewCell
        cell.backgroundColor = UIColor.blue
        print(dataSource[indexPath.item])
        cell.lable.text = dataSource[indexPath.item]
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
